package com.changpay.zywy.ui.fragment.service

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.AllowCommitAdapter
import com.changpay.zywy.base.BaseFragment
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.AllocLogBean
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.ui.activity.serivce.AllocMachineDetailActivity
import com.changpay.zywy.wight.CustomLoadMoreView
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.fragment_refresh_layout.*


class AllowCommitFragment : BaseFragment(),IView {
    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {

    }

    override fun showMsg(msg: String) {

    }

    override fun showError(errorMsg: String) {

    }

    private var position: Int = 0  // 当前viewPage的位置
    private var type: Int = 0;
    private var page = 1
    private val pagesize = "10"

    companion object {
        fun getInstance(instanceType: Int): AllowCommitFragment {
            val bundle = Bundle()
            bundle.putInt("position", instanceType)
            val fragment = AllowCommitFragment()
            fragment.arguments = bundle
            return fragment
        }
    }


    override fun attachLayoutRes(): Int = R.layout.fragment_refresh_layout

    override fun lazyLoad() {
        loadData()
    }

    override fun initView(view: View) {
        position = arguments?.getInt("position") ?: 0
        type =  position
        swipeRefreshLayout.isEnabled = false
        initRecyclerView()
    }

    /**
     * datas
     */
    private val datas = mutableListOf<AllocLogBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: AllowCommitAdapter by lazy {
        AllowCommitAdapter(context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }

    /**
     * LoadMoreListener
     */
    private val onRequestLoadMoreListener = BaseQuickAdapter.RequestLoadMoreListener {
        page = mAdapter.data.size / 10 + 1
        loadData()

    }

    private fun loadData() {

        val param = hashMapOf<String, String>()
        param["key"] = token
        param["state"] = type.toString()
        param["page"] = page.toString()
        param["pagesize"] = pagesize
        RetrofitHelper.service.get_sn_transfer_list(param).sss(this)
        {

            if(position == 1)
            {
                it.result.forEach { it.position = 1 }
            }

            mAdapter.run {
                addData(it.result)
                val size = it.result.size
                if (size < 10) {
                    loadMoreEnd(true)
                } else {
                    loadMoreComplete()
                }
            }
        }
    }


    /**
     * ItemClickListener
     */
    private val onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->


        if (datas.size != 0) {
            val data = datas[position]
            Intent(activity, AllocMachineDetailActivity::class.java).run {
                putExtra("transfer_id", data.transfer_id)
                if (this@AllowCommitFragment.position == 1)
                {
                    putExtra("hide", true)
                }
                startActivityForResult(this,100)
            }
        }
    }

    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
        if (datas.size != 0) {
            val data = datas[position]
            when (view.id) {
//                    R.id.iv_like -> {
//
//                    }
            }
        }
    }


    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView)
            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            setLoadMoreView(CustomLoadMoreView())
            onItemClickListener = this@AllowCommitFragment.onItemClickListener
            onItemChildClickListener = this.onItemChildClickListener
            setEmptyView(R.layout.fragment_empty_layout)
        }
    }

//    override fun onResume() {
//        super.onResume()
//
//
//
//    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == 200&&position ==0)
        {
            page = 1
            datas.clear()
            loadData()
        }
    }
}






